<?php
class ReserveModel extends CI_Model{
    public function __construct(){
        $this->load->database();
        parent:: __construct();
    }

    public function get($seatId, $shiftId){
        $data = array(
            'seatId'=>$seatId,
            'shiftId'=>$shiftId,
            'cancelTime'=> null
        );
        $query = $this->db->get_where("reservationlist", $data,1);
        return  $query->num_rows();
    }
    
    //get reservation list  by condition
    public function getList(){
        $where = array(
            'cancelTime'=>null,
        ); 

        $query = $this->db->get_where("reservationlist", $where,10);
        return $query->result_array();

    }


    public function add($userId, $seatId, $shiftId, $operationTime){
        $data= array(
            'userId'=>$userId, 
            'seatId'=>$seatId,
            'shiftId'=>$shiftId,
            'operationTime'=>$operationTime
        );
        $this->db->insert('reservationlist', $data); 

        return $this->db->affected_rows();
    }

    public function checkCancel($userId, $seatId, $shiftId){
        $data = array(
            'seatId'=>$seatId,
            'shiftId'=>$shiftId,
            'cancelTime' =>null,
        );
        $query = $this->db->get_where("reservationlist", $data,1);
        return  $query->num_rows();
    }

    public function cancel($userId, $seatId, $shiftId, $cancelTime){
        
        $data= array(
            'userId'=>$userId, 
            'seatId'=>$seatId,
            'shiftId'=>$shiftId,
            'cancelTime'=>null
        );
        $query = $this->db->update("reservationlist", array('cancelTime'=>$cancelTime), $data);
        return  $query;
    }

    public function updateViolaiton(){

    }

}
